//
//  KHWelcomeVC.m
//  KissHealth
//
//  Created by Mark on 2017/1/9.
//  Copyright © 2017年 Mark. All rights reserved.
//

#import "KHWelcomeVC.h"
#import "KHLoginVC.h"
#import "KHCommentVC.h"
#define HWNewfeatureCount 3

@interface KHWelcomeVC ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIScrollView *scrollView;

@end

@implementation KHWelcomeVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.创建一个scrollView：显示所有的新特性图片
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    // 2.添加图片到scrollView中
    CGFloat scrollW = scrollView.width;
    CGFloat scrollH = scrollView.height;
    for (int i = 0; i<HWNewfeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;
        // 显示图片
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        
        // 如果是最后一个imageView，就往里面添加其他内容
        if (i == HWNewfeatureCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
    
    // 3.设置scrollView的其他属性
    // 如果想要某个方向上不能滚动，那么这个方向对应的尺寸数值传0即可
    scrollView.contentSize = CGSizeMake(HWNewfeatureCount * scrollW, 0);
    scrollView.bounces = NO; // 去除弹簧效果
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    

   }

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.width;
    if (page == HWNewfeatureCount-1 ) {
        //[self performSelector:@selector(startClick) withObject:nil afterDelay:0.5];
    }

}

/**
 *  初始化最后一个imageView
 *
 *  @param imageView 最后一个imageView
 */
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 开启交互功能
    imageView.userInteractionEnabled = YES;
    
    // 1.进入
    UIButton *comeIn = [[UIButton alloc] init];
    [comeIn setTitle:@"进入" forState:UIControlStateNormal];
    [comeIn setTitleColor:[UIColor colorWithRed:0 green:183/255.0 blue:238/255.0 alpha:1] forState:UIControlStateNormal];
    [comeIn setTitleColor:[UIColor colorWithRed:0 green:128/255.0 blue:166/255.0 alpha:1] forState:UIControlStateHighlighted];
    comeIn.titleLabel.font = [UIFont systemFontOfSize:12];
    comeIn.width = 86;
    comeIn.height =70 ;

    comeIn.frame = CGRectMake(kSCREEN_W-comeIn.width, kSCREEN_H-comeIn.height, comeIn.width, comeIn.height);
    [comeIn addTarget:self action:@selector(comeInClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:comeIn];

}

- (void)comeInClick:(UIButton *)sender
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[KHLoginVC alloc]initWithURL:kLoginURLString];

}


@end
